import * as types from './mutation_type'
import topoApi from 'api/topo'

export default {
    // 设置当前拓扑图图标数据
    // getTopoIconData({commit}, sysmapid) {
    //   return new Promise((resolve, reject) => {
    //       topoApi.queryImageByMapid(sysmapid).then(response => {
    //         if (response.status == 200 && response.data.error == undefined) {
    //           commit(types.SUCCESS_SET_TOPOICONDATA, response.data)
    //           resolve(response)
    //         } else {
    //           commit(types.FAIL_SET_ERRORMESSAGE, response.data.error.data);
    //           reject(err)
    //         }
    //       }).catch(err => {
    //         commit(types.FAIL_SET_ERRORMESSAGE, err);
    //         reject(err)
    //       });
    //    });
    // },
    // 设置当前选中拓扑图元素数据
    setCurMapElement({ commit }, curMapElement) {
        commit(types.SUCCESS_SET_CURMAPELE, curMapElement)
    },
    // 设置当前选中拓扑图元素ID
    setCurElementId({ commit }, curElementId) {
        commit(types.SUCCESS_SET_CURMAPELEID, curElementId)
    },
    // 设置当前sysmapid
    setCurSysmapid({ commit }, sysmapid) {
        commit(types.SUCCESS_SET_SYSMAPID, sysmapid)
    },
    // 设置系统图标
    getSysIcons({ commit }) {
        return new Promise((resolve, reject) => {
            topoApi.querySysImage().then(response => {
                if (response.status == 200 && response.data.error == undefined) {
                    commit(types.SUCCESS_SET_SYSICONS, response.data)
                    resolve(response)
                } else {
                    commit(types.FAIL_SET_ERRORMESSAGE, response.data.error.data);
                    reject(response)
                }
            }).catch(err => {
                commit(types.FAIL_SET_ERRORMESSAGE, err);
                reject(err)
            });
        });
    },
    // 设置交换机接口数据
    getExchangerInterfaceData({ commit }) {
        return new Promise((resolve, reject) => {
            topoApi.queryInterface().then(response => {
                if (response.status == 200 && response.data.error == undefined) {
                    commit(types.SUCCESS_SET_EXCHANGER_INTERFACE_DATA, response.data)
                    resolve(response)
                } else {
                    commit(types.FAIL_SET_ERRORMESSAGE, response.data.error.data);
                    reject(response)
                }
            }).catch(err => {
                commit(types.FAIL_SET_ERRORMESSAGE, err);
                reject(err)
            });
        });
    },
    // 设置交换机主机数据
    getExchangerHostData({ commit }) {
        return new Promise((resolve, reject) => {
            topoApi.queryExchangerHost().then(response => {
                if (response.status == 200 && response.data.error == undefined) {
                    commit(types.SUCCESS_SET_EXCHANGER_HOST_DATA, response.data)
                    resolve(response)
                } else {
                    commit(types.FAIL_SET_ERRORMESSAGE, response.data.error.data);
                    reject(response)
                }
            }).catch(err => {
                commit(types.FAIL_SET_ERRORMESSAGE, err);
                reject(err)
            });
        });
    },
    // 根据id获取拓扑图数据
    getMapDataById({ commit }, sysmapid) {
        return new Promise((resolve, reject) => {
            topoApi.queryMapDataById(sysmapid).then(response => {
                if (response.status == 200 && response.data.error == undefined) {
                    resolve(response)
                } else {
                    commit(types.FAIL_SET_ERRORMESSAGE, response.data.error.data);
                    reject(response)
                }
            }).catch(err => {
                commit(types.FAIL_SET_ERRORMESSAGE, err);
                reject(err)
            });
        });
    },

    // 根据类型获取拓扑图基础数据
    listMapBaseDataByType({ commit }, type) {
        return new Promise((resolve, reject) => {
            topoApi.listMapByType(type).then(response => {
                if (response.status == 200 && response.data.error == undefined) {
                    resolve(response)
                } else {
                    commit(types.FAIL_SET_ERRORMESSAGE, response.data.error.data);
                    reject(response)
                }
            }).catch(err => {
                commit(types.FAIL_SET_ERRORMESSAGE, err);
                reject(err)
            });
        });
    },

    // 获取ip和交换机端口流量的关系
    getHostIdToIncomingAndOutgoingData({ commit }) {
        return new Promise((resolve, reject) => {
            topoApi.getHostIdToIncomingAndOutgoingData().then(response => {
                console.log('response', response)
                if (response.status == 200 && response.data.error == undefined) {
                    commit(types.SUCCESS_SET_EXCHANGER_PORTANDIP_DATA, response.data)
                    console.log('responsedata', response.data)
                    resolve(response)
                } else {
                    commit(types.FAIL_SET_ERRORMESSAGE, response.data.error.data);
                    reject(response)
                }
            }).catch(err => {
                commit(types.FAIL_SET_ERRORMESSAGE, err);
                reject(err)
            });
        });
    }
}